﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace ElingOA6_DSJCJ.DAL
{
    /// <summary>
    /// 问卷主题 数据类
    /// </summary>
    public class Paper
    {
        public Paper()
        {
        }
        /// <summary>
        /// 得到一个对象实体
        /// <creator>wangl</creator>
        /// <date>2013-09-11</date>
        /// </summary>
        public ElingOA6_DSJCJ.Model.Paper GetModel(decimal paperId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FPaperClsNo,FTitle,FContent,FIsRecordName,FBeginDate,FEndDate,FResultIsOpen,FPaperId,FEntityNo,FPubDate,FPubDeptNo,FPubEmpNo,FPubIp,FPaperType,FIsVoteIPControl,FIsLookIPControl from t_Paper ");
            strSql.Append(" where FPaperId=@FPaperId");
            SqlParameter[] parameters = {
					new SqlParameter("@FPaperId", SqlDbType.Decimal)
			};
            parameters[0].Value = paperId;

            ElingOA6_DSJCJ.Model.Paper model = new ElingOA6_DSJCJ.Model.Paper();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// <creator>wangl</creator>
        /// <date>2013-09-11</date>
        /// </summary>
        public ElingOA6_DSJCJ.Model.Paper DataRowToModel(DataRow row)
        {
            ElingOA6_DSJCJ.Model.Paper model = new ElingOA6_DSJCJ.Model.Paper();
            if (row != null)
            {
                if (row["FPaperClsNo"] != null)
                {
                    model.FPaperClsNo = row["FPaperClsNo"].ToString();
                }
                if (row["FTitle"] != null)
                {
                    model.FTitle = row["FTitle"].ToString();
                }
                if (row["FContent"] != null)
                {
                    model.FContent = row["FContent"].ToString();
                }
                if (row["FIsRecordName"] != null)
                {
                    model.FIsRecordName = row["FIsRecordName"].ToString();
                }
                if (row["FBeginDate"] != null && row["FBeginDate"].ToString() != "")
                {
                    model.FBeginDate = DateTime.Parse(row["FBeginDate"].ToString());
                }
                if (row["FEndDate"] != null && row["FEndDate"].ToString() != "")
                {
                    model.FEndDate = DateTime.Parse(row["FEndDate"].ToString());
                }
                if (row["FResultIsOpen"] != null)
                {
                    model.FResultIsOpen = row["FResultIsOpen"].ToString();
                }
                if (row["FPaperId"] != null && row["FPaperId"].ToString() != "")
                {
                    model.FPaperId = decimal.Parse(row["FPaperId"].ToString());
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FPubDate"] != null && row["FPubDate"].ToString() != "")
                {
                    model.FPubDate = DateTime.Parse(row["FPubDate"].ToString());
                }
                if (row["FPubDeptNo"] != null)
                {
                    model.FPubDeptNo = row["FPubDeptNo"].ToString();
                }
                if (row["FPubEmpNo"] != null)
                {
                    model.FPubEmpNo = row["FPubEmpNo"].ToString();
                }
                if (row["FPubIp"] != null)
                {
                    model.FPubIp = row["FPubIp"].ToString();
                }
                if (row["FPaperType"] != null)
                {
                    model.FPaperType = row["FPaperType"].ToString();
                }
                if (row["FIsVoteIPControl"] != null)
                {
                    model.FIsVoteIPControl = row["FIsVoteIPControl"].ToString();
                }
                if (row["FIsLookIPControl"] != null)
                {
                    model.FIsLookIPControl = row["FIsLookIPControl"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        ///  判断当前登陆人员是否有权限对指定问卷的查看权限
        /// <creator>wangl</creator>
        /// <date>2013-11-07</date>
        /// </summary>
        /// <param name="emplNo"></param>
        /// <param name="paperId"></param>
        /// <param name="entityNo"></param>
        /// <returns></returns>
        public bool IsPaperAuthorityForEmpl(string emplNo,string deptNo, decimal paperId, string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_Paper a inner join t_DWPaperIdDestAuthority b ");
            strSql.Append(" on a.FEntityNo = b.FEntityNo and ");
            strSql.Append(" a.FPaperId = b.FPaperId and ");
            strSql.Append("(b.FEmplNo='#ALL#' or (b.FDestDeptNo=@FDeptNo ");
            strSql.Append(" and b.FEmplNo=@FEmplNo) ) where  a.FEntityNo=@FEntityNo and a.FPaperId=@FPaperId ");

            SqlParameter[] parameters = {
                    new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
                    new SqlParameter("@FDeptNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FPaperId", SqlDbType.Decimal,9),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = emplNo;
            parameters[1].Value = deptNo;
            parameters[2].Value = paperId;
            parameters[3].Value = entityNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
    }
}
